import _ from "lodash";

// 渲染参数
export function renderParams(type, data) {
  let params = { ...data };
  delete params.oldPhone;
  delete params.code;
  delete params.oldPassword;
  delete params.password;
  delete params.confirmPassword;
  delete params.industry;
  delete params.qrCode;
  if (type === "username") {
    params = {
      ...params,
      accountModifyStatus: 1
    };
  }
  return params;
}

/* 方法说明
 * @method getOptionsByValue 通过选中值，返回完整选中项数据的集合
 * @param {keyName, options checkedOptions，value}
 *    keyName: 用于筛选的属性名
 *    options: 是数据源, 类型：array
 *    checkedOptions: 每次调用方法得到的数据，并用于下一次调用，类型：array
 *    key: 选中项value， 类型：array
 * @return {checkedOptions} 完整选中项数据的集合， 类型：array
 */
export function getOptionsByValue(
  options,
  checkedOptions = [],
  key,
  keyName = "key"
) {
  if (!options || !options.length) return checkedOptions;
  options.forEach(item => {
    if (Array.isArray(item)) {
      getOptionsByValue(item, checkedOptions, key, keyName);
    }
    if (key && key.length > 0 && key.includes(item[keyName])) {
      checkedOptions.push(item);
    }
    getOptionsByValue(item.children, checkedOptions, key, keyName);
  });
  return checkedOptions;
}

// 获取数据源中最深层选中项
export function getSelectedList(options, selectedList) {
  if (!options || !options.length) return [];
  options.forEach(item => {
    if (!item.children || !item.children.length) {
      if (item.selected) {
        selectedList.push(item);
      }
    }
    getSelectedList(item.children, selectedList);
  });
  return selectedList;
}

// 获取数据源所有的key
export function getValues(option = [], values = []) {
  if (!option || !option.length) return values;
  option.forEach(item => {
    values.push(item.key);
    if (item.children && item.children.length) {
      getValues(item.children, values);
    }
  });
  return values;
}

// 初始化数据源最深层选中状态
export function initSelect(that, options, selectedSet, allowSet) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    if (option.children && option.children.length) {
      initSelect(that, option.children, selectedSet, allowSet);
    } else {
      option.disabled = allowSet.size ? !allowSet.has(option.key) : false;
      option.selected = selectedSet.has(option.key);
      that.$set(option, "indeterminate", false);
    }
  });
}

// 初始化数据源，给含有(selected或者indeterminate值为true)的子级的父级添加indeterminate属性
export function initParentIndet(
  that,
  options,
  selectedSet,
  allowSet,
  fullKey = ""
) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    option.fullKey = fullKey ? fullKey + "," + option.key : option.key;
    if (option.children && option.children.length) {
      initParentIndet(that, option.children, selectedSet, allowSet, fullKey);
      option.selected = selectedSet.has(option.key);
      const indeterminate =
        selectedSet.has(option.key) ||
        option.children.some(item => item.selected || item.indeterminate);
      const disabled = option.selected
        ? false
        : allowSet.size
        ? !allowSet.has(option.key)
        : option.children.every(item => item.disabled);
      that.$set(option, "disabled", disabled);
      that.$set(option, "indeterminate", indeterminate);
    }
  });
}

// 改变数据源的选中状态
export function setSelected(that, options, selectedSet) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    option.selected = selectedSet.has(option.key);
    if (option.children && option.children.length) {
      setSelected(that, option.children, selectedSet);
    } else {
      that.$set(option, "indeterminate", false);
    }
  });
}

// 改变数据源父级的选中状态
export function setParentIndet(that, options, selectedSet) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    if (option.children && option.children.length) {
      setParentIndet(that, option.children, selectedSet);
      const indeterminate =
        selectedSet.has(option.key) ||
        option.children.some(item => item.selected || item.indeterminate);
      that.$set(option, "indeterminate", indeterminate);
    }
  });
}

// 获取层级selected或者indeterminate值为true的数据
export function getLevelOptions(
  options,
  maxLevel,
  level = 0,
  showOptions = []
) {
  if (!options || !options.length || level > maxLevel) return showOptions;
  let option = showOptions[level] || [];
  options.forEach(item => {
    if (item.selected || item.indeterminate) {
      option.push(item);
    }
    if (item.children && item.children.length) {
      getLevelOptions(item.children, maxLevel, level + 1, showOptions);
    }
  });
  showOptions[level] = option;
  return showOptions;
}

export function flattenOptions(options, level, useFilter = false) {
  if (!options || !options.length) return [];
  const data = options.map(item => {
    item.isTitle = level;
    let children = item.children || [];
    if (useFilter) {
      children = children.length
        ? children.filter(sub => sub.filtered)
        : children;
    }
    return children.length ? [item].concat(children) : [];
  });
  return _.flatten(data);
}

// 清空数据源的过滤状态
export function setFiltered(options) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    option.filtered = false;
    if (option.children && option.children.length) {
      setFiltered(option.children);
    }
  });
}

// 改变数据源中父级的过滤状态
export function setParentFiltered(that, options, keyword, level = 0) {
  if (!options || !options.length) return options;
  options.forEach(option => {
    if (option.children && option.children.length) {
      setParentFiltered(that, option.children, keyword, level + 1);
      let filtered = false;
      if (level === 0) {
        filtered = true;
      }
      filtered =
        option.value.includes(keyword) ||
        (level === 1
          ? option.selected && option.children.some(item => item.filtered)
          : option.children.some(item => item.filtered));
      that.$set(option, "filtered", filtered);
    }
  });
}

// 获取层级filtered为true的数据
export function getFilteredOptions(
  options,
  maxLevel,
  level = 0,
  showOptions = []
) {
  if (!options || !options.length || level > maxLevel) return showOptions;
  let option = showOptions[level] || [];
  options.forEach(item => {
    if (item.filtered) {
      option.push(item);
    }
    if (item.children && item.children.length) {
      getFilteredOptions(item.children, maxLevel, level + 1, showOptions);
    }
  });
  showOptions[level] = option;
  return showOptions;
}

export function getLevelOptionsByValues(options, list, values = [], level = 0) {
  if (!list || !list.length) return [];
  let value = values[level] || [];
  options.forEach(item => {
    if (list.includes(item.key)) {
      value.push(item);
    }
    if (item.children && item.children.length) {
      getLevelOptionsByValues(item.children, list, values, level + 1);
    }
  });
  values[level] = value;
  return values;
}

export function getMaxDeepValues(options, values = []) {
  if (!options || !options.length) return values;
  options.forEach(item => {
    if (item.children && item.children.length) {
      getMaxDeepValues(item.children, values);
    } else {
      values.push(item.key);
    }
  });
  return values;
}

// 获取层级selected值为true的数据
export function getSelectOptions(options, level = 0, showOptions = []) {
  if (!options || !options.length) return showOptions;
  let option = showOptions[level] || [];
  options.forEach(item => {
    if (item.selected) {
      option.push(item);
    }
    if (item.children && item.children.length) {
      getSelectOptions(item.children, level + 1, showOptions);
    }
  });
  showOptions[level] = option;
  return showOptions;
}
